package javaforce.utils;

/** Tests Camera function (to be deleted)
 *
 * @author pquiring
 *
 * Created : Jun 9, 2014
 */
import java.util.*;

import javaforce.*;
import javaforce.media.*;

public class TestCamera extends javax.swing.JFrame {

  /**
   * Creates new form Test2
   */
  public TestCamera() {
    initComponents();
    setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    preview = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Camera Test");

    jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    jButton1.setText("Start");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    jButton2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    jButton2.setText("Stop");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton2ActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(preview, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addGroup(layout.createSequentialGroup()
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 369, Short.MAX_VALUE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
          .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(preview, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    camera = new Camera();
    if (camera == null) {
      JFLog.log("cam == null");
      return;
    }
    if (!camera.init()) {
      JFLog.log("init failed");
      return;
    }
    String devices[] = camera.listDevices();
    if (devices == null || devices.length == 0) {
      JFLog.log("no devices found");
      return;
    }
    JFLog.log("device count=" + devices.length);
    for (int a = 0; a < devices.length; a++) {
      JFLog.log("device=" + devices[a]);
    }
    camera.start(0, 640, 480);
    width = camera.getWidth();
    height = camera.getHeight();
    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
        java.awt.EventQueue.invokeLater(new Runnable() {public void run() {
          int px[] = camera.getFrame();
          if (px == null) return;  //not ready
          if (img == null) {
            img = new JFImage(width, height);
          }
          System.arraycopy(px, 0, img.getBuffer(), 0, width * height);
/*          cnt++;
          if (cnt > 5) {
            img.savePNG("test" + cnt + ".png");
          }*/
          preview.setIcon(img);
          preview.repaint();
        }});
      }
    }, 500, 500);
  }//GEN-LAST:event_jButton1ActionPerformed

  int cnt = 0;

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    if (camera == null) {
      return;
    }
    timer.cancel();
    timer = null;
    camera.stop();
    camera = null;
  }//GEN-LAST:event_jButton2ActionPerformed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new TestCamera().setVisible(true);
      }
    });
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JLabel preview;
  // End of variables declaration//GEN-END:variables

  private Camera camera;
  private Timer timer;
  private int frame;
  private JFImage img;
  private int width, height;
}
